<?php
class MemberModel extends DbModel {
	/**
	 * 添加新用户到数据库.
	 * @param	array	$data
	 * @return boolean 
	 */
	public function addMember($data) {
		foreach ($data as $d_key => $d_val) $data[$d_key] = mysql_real_escape_string($d_val);
		$data['password'] = md5($data['password']);
		$sql = 'INSERT INTO `'.$this->db_prefix.'member` ';
		$sql .= '(`'.implode('`,`', array_keys($data)).'`) VALUES ("'.implode('","', array_values($data)).'")';
		
		if ($this->db->exec($sql)) {
			$member_id = $this->db->lastInsertId();
			$this->db->exec('INSERT INTO `'.$this->db_prefix.'member_info` (`member_id`) VALUES ("'.$member_id.'")');
			return true;
		}
		
		return false;
	}
	
	/**
	 * 检查该用户名的用户是否已经存在.
	 * @param	string	$username
	 * @return 	boolean 
	 */
	public function unameExist($username) {
		$sql = 'SELECT `uname` FROM `'.$this->db_prefix.'member` WHERE `uname` = "'.mysql_real_escape_string($username).'"';
		$data = $this->db->findOne($sql);
		
		return isset($data['uname']) ? true : false;
	}
	
	/**
	 * 检查用户昵称是否已经存在
	 * @param	string	$nickname
	 * @return	boolean 
	 */
	public function nicknameExist($nickname) {
		$sql = 'SELECT `nickname` FROM `'.$this->db_prefix.'member` WHERE `nickname` = "'.mysql_real_escape_string($nickname).'"';
		$data = $this->db->findOne($sql);
		
		return isset($data['nickname']) ? true : false;
	}
	
	/**
	 * 用户登录.
	 * @param	string	$username
	 * @param	string	$password
	 * @return	boolean 
	 */
	public function login($username, $password) {
		$sql = 'SELECT `member_id`,`uname`,`sex`,`nickname`,`local_city` FROM `'.$this->db_prefix.'member` WHERE `uname` = "'
			.mysql_real_escape_string($username).'" AND `password` = "'.md5($password).'"';
		
		$data = $this->db->findOne($sql);
		
		return isset($data['uname']) ? $data : FALSE;
	}
	
	/**
	 * 取得个人资料. 
	 */
	public function getBasicInfo($member_id) {
		$sql = 'SELECT `member_id`,`uname`,`sex`,`brief`,`nickname`,`local_city` FROM `'
			.$this->db_prefix.'member` WHERE `member_id` ='.$member_id;
		
		return $this->db->findOne($sql);
	}
	
	/**
	 * 更新表数据. 
	 * @param	array	$member_data
	 * @param	integer $member_id
	 * @return  boolean
	 */
	public function updateMember($member_data, $member_id) {
		$sql = 'UPDATE `'.$this->db_prefix.'member` SET ';
		foreach ($member_data as $d_key => $d_val) {
			$sql .= $d_key.'="'.mysql_real_escape_string($d_val).'",';
		}
		
		$sql = substr($sql, 0, strlen($sql)-1) . 'WHERE `member_id` = '.$member_id;
		return $this->db->exec($sql);
	}
	
	/**
	 * 更新用户详细信息.
	 * @param	array	$data
	 * @param 	integer $member_id
	 * @return  boolean
	 */
	public function updateMemberInfo($data, $member_id) {
		$sql = 'UPDATE `'.$this->db_prefix.'member_info` SET ';
		foreach ($data as $d_key => $d_val) {
			$sql .= $d_key.'="'.mysql_real_escape_string($d_val).'",';
		}
		
		$sql = substr($sql, 0, strlen($sql)-1) . 'WHERE `member_id` = '.$member_id;
		return $this->db->exec($sql);
	}
	
	/**
	 * 获取用户详细信息.
	 * @param	integer		$member_id
	 * @return	array 
	 */
	public function getMemberInfo($member_id) {
		$sql = 'SELECT * FROM `'.$this->db_prefix.'member_info` WHERE `member_id` = '.$member_id;
		
		return $this->db->findOne($sql);
	}
}